﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class megahealth_search : System.Web.UI.Page
{
    string search = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["user"] == null)
        {
            Response.Redirect("../login.aspx");
        }
        else
        {
            if (!Page.IsPostBack)
            {
                //lsearch.Text = "<li class='cat_hold'><a href='#'><img src='image/not_found.jpg' alt='MegaHealth' /><span class='info'>Nombre Producto (0)</span></a></li>";
                if (Request.QueryString["get"] != null && Request.QueryString["get"].ToString() != "")
                {
                    search = Request.QueryString["get"].ToString();
                    if (!search.Contains(" "))
                    {
                        if (search.Contains("-"))
                        {
                            search = search.Replace("-", "");
                            buscar(" (nombre collate SQL_Latin1_General_Cp1_CI_AI LIKE '" + search + "%') ", null, false);
                        }
                        else
                        {
                            buscar(" (id = '" + search + "') OR (tags collate SQL_Latin1_General_Cp1_CI_AI LIKE '%," + search + "%') ", null, false);
                            buscar(" (beneficios collate SQL_Latin1_General_Cp1_CI_AI LIKE '%[^a-z]" + search + "%') OR (recomendaciones collate SQL_Latin1_General_Cp1_CI_AI LIKE '%[^a-z]" + search + "%') OR (beneficios collate SQL_Latin1_General_Cp1_CI_AI LIKE '" + search + "%') OR (recomendaciones collate SQL_Latin1_General_Cp1_CI_AI LIKE '" + search + "%') ", null, true);
                        }
                    }
                    else
                    {
                        string[] auxSearch = search.Split(' ');
                        string SBEAND = "", SREAND = "", SBEOR = "", SREOR = "", STAND = "";
                        for (int i = 0; i < auxSearch.Length; i++)
                        {
                            if (i < auxSearch.Length - 1)
                            {
                                if (auxSearch[i] != "")
                                {
                                    SBEAND += " (beneficios collate SQL_Latin1_General_Cp1_CI_AI LIKE '%" + auxSearch[i] + "%') AND ";
                                    SREAND += " (recomendaciones collate SQL_Latin1_General_Cp1_CI_AI LIKE '%" + auxSearch[i] + "%') AND ";

                                    SBEOR += " (beneficios collate SQL_Latin1_General_Cp1_CI_AI LIKE '%" + auxSearch[i] + "%') OR ";
                                    SREOR += " (recomendaciones collate SQL_Latin1_General_Cp1_CI_AI LIKE '%" + auxSearch[i] + "%') OR ";

                                    STAND += " (tags collate SQL_Latin1_General_Cp1_CI_AI LIKE '%," + auxSearch[i] + "%') AND ";
                                }
                            }
                            else
                            {
                                SBEAND += " (beneficios collate SQL_Latin1_General_Cp1_CI_AI LIKE '%" + auxSearch[i] + "%') ";
                                SREAND += " (recomendaciones collate SQL_Latin1_General_Cp1_CI_AI LIKE '%" + auxSearch[i] + "%') ";

                                SBEOR += " (beneficios collate SQL_Latin1_General_Cp1_CI_AI LIKE '%" + auxSearch[i] + "%') ";
                                SREOR += " (recomendaciones collate SQL_Latin1_General_Cp1_CI_AI LIKE '%" + auxSearch[i] + "%') ";

                                STAND += " (tags collate SQL_Latin1_General_Cp1_CI_AI LIKE '%," + auxSearch[i] + "%') ";
                            }
                        }
                        buscar("(" + STAND + ")", null, false);
                        buscar("(" + SBEAND + ") OR (" + SREAND + ")", "(" + SBEOR + ") OR (" + SREOR + ")", true);
                    }
                    ltitle1.Text = "Megahealth"; ltitle2.Text = "Busqueda Relacionada";
                    ltitle1.Visible = true; ltitle2.Visible = true;
                }
            }
        }
    }

    protected void lnk_search_Click(object sender, EventArgs e)
    {
        if (txsearch.Text != "")
        {
            string searchBT = txsearch.Text;
            if (!searchBT.Contains("+"))
            {
                if (searchBT.Contains("-"))
                {
                    searchBT = searchBT.Replace("-", "");
                    buscar(" (nombre collate SQL_Latin1_General_Cp1_CI_AI LIKE '" + searchBT + "%') ", null, false);
                }
                else
                {
                    buscar(" (id = '" + searchBT + "') OR (tags collate SQL_Latin1_General_Cp1_CI_AI LIKE '%," + searchBT + "%') ", null, false);
                    buscar(" (beneficios collate SQL_Latin1_General_Cp1_CI_AI LIKE '%[^a-z]" + searchBT + "%') OR (recomendaciones collate SQL_Latin1_General_Cp1_CI_AI LIKE '%[^a-z]" + searchBT + "%') OR (beneficios collate SQL_Latin1_General_Cp1_CI_AI LIKE '" + searchBT + "%') OR (recomendaciones collate SQL_Latin1_General_Cp1_CI_AI LIKE '" + searchBT + "%') ", null, true);
                }
            }
            else
            {
                string[] auxSearch = searchBT.Split('+');
                string SBEAND = "", SREAND = "", SBEOR = "", SREOR = "", STAND = "";
                for (int i = 0; i < auxSearch.Length; i++)
                {
                    if (i < auxSearch.Length - 1)
                    {
                        if (auxSearch[i] != "")
                        {
                            SBEAND += " (beneficios collate SQL_Latin1_General_Cp1_CI_AI LIKE '%[^a-z]" + auxSearch[i] + "%') AND ";
                            SREAND += " (recomendaciones collate SQL_Latin1_General_Cp1_CI_AI LIKE '%[^a-z]" + auxSearch[i] + "%') AND ";

                            SBEOR += " (beneficios collate SQL_Latin1_General_Cp1_CI_AI LIKE '%[^a-z]" + auxSearch[i] + "%') OR ";
                            SREOR += " (recomendaciones collate SQL_Latin1_General_Cp1_CI_AI LIKE '%[^a-z]" + auxSearch[i] + "%') OR ";

                            STAND += " (tags collate SQL_Latin1_General_Cp1_CI_AI LIKE '%," + auxSearch[i] + "%') AND ";
                        }
                    }
                    else
                    {
                        SBEAND += " (beneficios collate SQL_Latin1_General_Cp1_CI_AI LIKE '%[^a-z]" + auxSearch[i] + "%') ";
                        SREAND += " (recomendaciones collate SQL_Latin1_General_Cp1_CI_AI LIKE '%[^a-z]" + auxSearch[i] + "%') ";

                        SBEOR += " (beneficios collate SQL_Latin1_General_Cp1_CI_AI LIKE '%[^a-z]" + auxSearch[i] + "%') ";
                        SREOR += " (recomendaciones collate SQL_Latin1_General_Cp1_CI_AI LIKE '%[^a-z]" + auxSearch[i] + "%') ";

                        STAND += " (tags collate SQL_Latin1_General_Cp1_CI_AI LIKE '%," + auxSearch[i] + "%') ";
                    }
                }
                buscar("(" + STAND + ")", null, false);
                //searchBT = "(" + SBEAND + ") OR (" + SREAND + ") OR (" + SBEOR + ") OR (" + SREOR + ")";
                buscar("(" + SBEAND + ") OR (" + SREAND + ")", "(" + SBEOR + ") OR (" + SREOR + ")", true);
            }
            ltitle1.Text = "Megahealth"; ltitle2.Text = "Busqueda Relacionada";
            ltitle1.Visible = true; ltitle2.Visible = true;
        }
    }

    protected void buscar(string parametro1, string parametro2, bool bandera)
    {
        string productos = "<ul class='sub_cats'>";

        if (parametro1 != "")
        {
            DataSet busqueda = DB.getProductosBusqueda(parametro1);
            DataSet busqueda2 = null;
            string estilo = "";
            if (parametro2 != null)
            {
                parametro2 = " id NOT IN ( SELECT id FROM producto WHERE " + parametro1 + ") AND (" + parametro2 + ")";
                busqueda2 = DB.getProductosBusqueda(parametro2);
                estilo = "style='border:5px solid #223B13; background:#73925E'";
            }

            if (busqueda != null && busqueda.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < busqueda.Tables[0].Rows.Count; i++)
                {
                    productos += "<li class='cat_hold' " + estilo + "><a href='producto.aspx?get=" + busqueda.Tables[0].Rows[i]["id"].ToString() + "'>";
                    productos += "<img src='image/productos_mini/" + busqueda.Tables[0].Rows[i]["foto"].ToString() + "' alt='MegaHealth'  class='image_Search' />";
                    productos += "<span class='name'>" + busqueda.Tables[0].Rows[i]["nombre"].ToString() + "</span>";
                    productos += "<span class='info'>" /*+ busqueda.Tables[0].Rows[i]["id"].ToString() + "<br />" */+ busqueda.Tables[0].Rows[i]["nombre"].ToString() + "</span>";
                    productos += "</a></li>";
                }
            }
            if (busqueda2 != null && busqueda2.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < busqueda2.Tables[0].Rows.Count; i++)
                {
                    productos += "<li class='cat_hold'><a href='producto.aspx?get=" + busqueda2.Tables[0].Rows[i]["id"].ToString() + "'>";
                    productos += "<img src='image/productos_mini/" + busqueda2.Tables[0].Rows[i]["foto"].ToString() + "' alt='MegaHealth'  class='image_Search' />";
                    productos += "<span class='name'>" + busqueda2.Tables[0].Rows[i]["nombre"].ToString() + "</span>";
                    productos += "<span class='info'>" /*+ busqueda2.Tables[0].Rows[i]["id"].ToString() + "<br />" */+ busqueda2.Tables[0].Rows[i]["nombre"].ToString() + "</span>";
                    productos += "</a></li>";
                }
            }
            
        }

        productos += "</ul>";
        if (bandera == false)
        {
            if (productos != "<ul class='sub_cats'></ul>")
                lsearch.Text = productos;
            else
                lsearch.Text = "<li class='cat_hold'><a href='#'><img src='image/nofound2.png' alt='MegaHealth' /><span class='name'>Ningun Resultado</span><span class='info'>Ningun Resultado</span></a></li>";
        }
        else
        {
            if (productos != "<ul class='sub_cats'></ul>")
                lsearch2.Text = productos;
            else
                lsearch2.Text = "<li class='cat_hold'><a href='#'><img src='image/nofound2.png' alt='MegaHealth' /><span class='name'>Ningun Resultado</span><span class='info'>Ningun Resultado</span></a></li>";
        }
    }
}